<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="shortcut icon" href="../images/yc.png" type="image/x-icon">
<link rel="stylesheet" href="../css/layui.css" media="all">
</head>
<body>
	<br />
	<form class="layui-form" id="myform" style="width: 90%"
		onsubmit="return false">
		<div class="layui-form-item">
			<label class="layui-form-label">公告标题</label>
			<div class="layui-input-inline" style="width: 25%">
				<input type="text" name="title" lay-verify="required"
					lay-reqtext="标题不能为空..." placeholder="请输入标题" autocomplete="off"
					class="layui-input" style="width: 400px;" id="title">
			</div>
		</div>
		<div class="layui-form-item" style="margin-left: 20px;" id="showpic"></div>
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">帖子详细</label>
				<div class="layui-input-block">
					<textarea name="goods_form_descr" id="goods_form_descr" rows="10" cols="80"></textarea>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button type="submit" class="layui-btn" lay-submit=""
					lay-filter="demo1">保存修改</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>

<script type="text/javascript" src="../js/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="../js/layui.all.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/showpic.js" charset="utf-8"></script>
<script type="text/javascript" src="../js/axios.js" charset="utf-8"></script>
<script type="text/javascript" src="../ckeditor/ckeditor.js"></script>
<script>
// CKEDITOR.replace('goods_form_descr');
layui.use(['form'], function(){
	var form = layui.form,
	layer = layui.layer;
	//获取要修改的信息
	let upId = sessionStorage.getItem("upId")
	let cbt = {}
	$.get("/tender/thisTenderInfo",{id:upId}).then(resp=>{
		let item = resp.data
		cbt =  resp.data
		console.log(item)
		$("#bdNo").val(item.bdNo)
		$("#title").val(item.title)
		// 初始化 CKEditor
		let editor = CKEDITOR.replace('goods_form_descr');
		// 等待 CKEditor 实例准备好后设置内容
		editor.on('instanceReady', function () {
			this.setData(item.content);
		});
	},"json")


	// 自定义验证规则
	form.verify({
    	title: function(value){
      		if(value.length < 2){
        		return '帖子名称至少得2个字符...';
      		}
    	}
	});

	// 监听提交
  	form.on('submit(demo1)', function(data){
  		let obj = data.field;
		let descr = CKEDITOR.instances.goods_form_descr.getData();
		obj.content = descr;
		obj.id = cbt.id
		obj.tno = cbt.tno
		$.ajaxFileUpload({
			url:"/tender/update",
			secureuri:false,
			fileElementId:"fileName",
			data:obj,
			dataType:"json",
			success:function(rt,status){
				if(rt.code == 511){
					layer.msg("输入信息不完整,请确认后提交...");
					return false;
				}

				if(rt.code == 200){
					$("#myform")[0].reset();   //重置表单
					$("#showpic").html("");  //清空图片预览
					CKEDITOR.instances.goods_form_descr.setData("");
					layui.form.render();
					layer.msg("修改成功...",{icon:1,time:2000},function (){
						sessionStorage.removeItem("upId")
						location.href="tenderAnnouncement.html"
					});
					return false;
				}
				layer.msg("修改失败...",{icon:2,time:2000});
			},
			error:function(rt,status,e){
				console.log(data)
				layer.msg("修改失败\n" + e,{icon:2,time:2000})
			}
		})
		return false;
	});
});
</script>
</body>
</html>